package com.yyk.yykywd.mvvm.ui

import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import com.yyk.yykywd.*
import com.yyk.yykywd.base.NewBaseActivity
import com.yyk.yykywd.databinding.ActivitySearchCustomerBinding
import com.yyk.yykywd.mvvm.adapter.SearchCustomerAdapter
import com.yyk.yykywd.mvvm.event.FinishWorkDetail
import com.yyk.yykywd.mvvm.view_model.SearchCustomerViewModel
import com.yyk.yykywd.util.ToAnotherActivity
import kotlinx.android.synthetic.main.activity_search_customer.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class SearchCustomerActivity : NewBaseActivity() {
    var type: String = ""
    private val viewModel by lazy {
        getViewModel(this, SearchCustomerViewModel::class.java)
    }
    private val adapter by lazy {
        SearchCustomerAdapter()
    }

    override fun initView() {
        EventBus.getDefault().register(this)
        val binding: ActivitySearchCustomerBinding =
                DataBindingUtil.setContentView(this,
                        R.layout.activity_search_customer)
        binding.viewModel = viewModel
        binding.lifecycleOwner = this
        viewModel.type = intent.getStringExtra(TYPE)

        rv.adapter = adapter

        loading_layout.showState(R.drawable.pic_wssdgkh, getString(R.string.search_text1))
        loading_layout.setStateMargin(resources.getDimension(R.dimen.dp_20).toInt())
        loading_layout.setStateTextColor(ContextCompat.getColor(mActivity, R.color.black_33))
    }

    override fun setListener() {
        viewModel.searchInfo.observe(this, Observer {
            hideRefreshAndLoadMore(srl)
            adapter.searchText = viewModel.searchText
            adapter.submitList(viewModel.mList)
            adapter.notifyDataSetChanged()

            if (viewModel.mList.isEmpty()) {

                if (viewModel.searchText.isEmpty()) {
                    loading_layout.showState(R.drawable.pic_wssdgkh, getString(R.string.search_text1))
                } else {
                    loading_layout.showEmpty(R.drawable.pic_wssdgkh, "未搜索到该客户")
                }
            }
        })

        srl.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                viewModel.loadMore()
                viewModel.searchInfo()
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                with(viewModel) {
                    refresh(mList)
                    searchInfo()
                }
            }
        })

        iv_back.setOnClickListener {
            finish()
        }
        tv_cancel.setOnClickListener {
            finish()
        }

        adapter.setOnItemChildClickListener { position, view ->
            with(viewModel.mList[position]) {
                when (viewModel.type) {
                    POTENTIAL_CUSTOMER -> {
                        if (client_type == 1) {
                            ToAnotherActivity.toReturnMoneyDetailActivity(mActivity, FROM_POTENTIAL_CUSTOMER_HOSPITAL,
                                    id.toString(), 0, 1, 0)
                        } else {
                            ToAnotherActivity.toReturnMoneyDetailActivity(mActivity, FROM_POTENTIAL_CUSTOMER_AGENT,
                                    id.toString(), 0, 1, 0)
                        }
                    }
                    SIGN_CONTRACT -> {
                        if (client_type == 1) {
                            ToAnotherActivity.toReturnMoneyDetailActivity(mActivity, FROM_SIGN_CONTRACT_HOSPITAL,
                                    id.toString(), 0, 1, 0)
                        } else {
                            ToAnotherActivity.toDetailActivity(mActivity,
                                    FROM_SIGN_CONTRACT_AGENT,
                                    id.toString(), show_type)
                        }
                    }
                }
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun finishDetail(event: FinishWorkDetail) {
        with(viewModel) {
            refresh(mList)
            searchInfo()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }
}
